<template>
	<!-- 添加我的素材 -->
	<view class="addmaterial">
		<!-- 单选 -->
		<view class="tabs" v-if="list1 && list1.length>=2">
			<u-tabs :inactiveStyle="{'fontSize':'34rpx'}" :activeStyle="{'fontSize':'34rpx'}" :list="list1" :scrollable="false" lineBgSize="cover" @click="clickTabs"></u-tabs>
		</view>
		<!-- 内容 -->
		<view class="contentBox">
			<!-- 图片 -->
			<view @click="chooseImg" v-if="currentIndex==0" style="margin-top: 20rpx;height:400rpx">
				<u-image v-if="updataImage" width="680rpx" :src="'https://www.ydylmold.cn/file/getPlayResource/108/'+updataImage" height="400rpx"></u-image>
				<image v-else src="https://www.ydylmold.cn/file/getPlayResource/108/upload.png" style="width: 80px;height: 20px;margin-top: 100px"></image>
			</view>
			<!-- 文本 -->
			<view v-if="currentIndex==1" style="width: 90%; margin-top: 20rpx;">
				<textarea v-model="areacontent" confirmType="done" placeholder="请输入内容" maxlength="128" count autoHeight style="background-color: #F4F5F7;width: 100%;min-height: 400rpx; padding: 30rpx;box-sizing: border-box;"></textarea>
			</view>
		</view>
		<!-- 按钮 -->
		<view style="display: flex;align-items: center;justify-content: center;">
			<view class="btn-news" @click="$u.throttle(saveClick, 1000)">保存</view>
		</view>
	</view>
</template>
<script>
	import {
		uploadFilePromise
	} from "@/utils/upload.js"
	import {
		getToken
	} from '@/utils/token.js'
	import {
		savePortalNewsSource
	} from '@/common/api/news.js'
	export default {

		data() {
			return {
				areacontent: '',
				list1: [{
					name: '图片',
				}, {
					name: '文本',
				}],
				currentIndex: 0,
				updataImage: '',
				optionItem: '',
				tempFilePath: ''
			}
		},

		onLoad(option) {
			if (option.item) {
				this.optionItem = JSON.parse(decodeURIComponent(option.item))
				if (this.optionItem.content.length > 0) {
					this.currentIndex = 1
					this.areacontent = this.optionItem.content
				}
				if (this.optionItem.imagesPath.length > 0) {
					this.currentIndex = 0
					this.updataImage = this.optionItem.imagesPath
				}
			}
		},
		methods: {
			clickTabs(data) {
				this.currentIndex = data.index
				//console.log("this.currentIndex", this.currentIndex)
			},
			chooseImg() {
				uni.chooseImage({
					count: 1,
					success: (e) => {
						//console.log(e,888)
						this.confirmImg(e)
					}
				})
			},
			async confirmImg(e) {
				//截图工具点击确定之后上传图片
				//如果不为空的话上传之后裁剪又会跳出来
				this.tempFilePath = ''
				let params = {
					params: {}
				}
				params['params']['fileType'] = 100
				params['params']['fileCode'] = 108
				params['url'] = e.tempFilePaths[0]
				const result = await uploadFilePromise(params)
				this.updataImage = result
			},

			saveClick() {
				if (!this.updataImage && this.currentIndex == 0) {
					this.showtost('请选择图片')
					return
				}
				if (!this.areacontent && this.currentIndex == 1) {
					this.showtost('请输入文本')
					return
				}
				let params = {}
				params['craftsmanId'] = getToken('useNum')
				if (getToken('newsCompany')) {
					params['companyId'] = getToken('newsCompany')
				}
				if (this.currentIndex == 0) {
					params['imagesPath'] = this.updataImage
					params['content'] = ''
				} else {
					params['imagesPath'] = ''
					params['content'] = this.areacontent
				}
				savePortalNewsSource(params).then(res => {
					//console.log('保存成功',res)
					res = res.data
					if (res.code == 200) {
						this.areacontent = ''
						this.showtost('保存成功！')
						uni.navigateTo({
							url: '/pages/newsPart/index/mymaterial'
						})
						this.$store.commit('isFresh', true)
					}
				})
			},
			showtost(title) {
				uni.showToast({
					title: title,
					icon: 'none',
					duration: 2000
				})
			}
		}
	}
</script>
<style lang="scss">
	.addmaterial {
		min-height: calc(100vh - var(--window-top));

		.contentBox {
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 50rpx;
		}

		.btn-news {
			background-color: #2E9AFF;
			border-radius: 10rpx;
			padding: 10rpx 100rpx;
			color: white;
			font-size: 32rpx;
			font-weight: 600;
			width: 80%;
			text-align: center;
			line-height: 60rpx;
		}

	}
</style>